package com.yj.springbootmybatis.rest;

import com.yj.springbootmybatis.pojo.*;
import com.yj.springbootmybatis.pojo.vo.CommentLevelCountsVO;
import com.yj.springbootmybatis.pojo.vo.ShopcartVO;
import com.yj.springbootmybatis.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

@Api(value = "商品接口",tags = "商品接口信息")
@RestController("items")
@Slf4j
public class ItemsController {

    @Autowired
    private ItemService itemService;




    @GetMapping("info/{itemId}")
    @ApiOperation(value = "根据商品id查询商品信息",httpMethod = "GET")
    public Map findItemsInfoByItemId(@PathVariable Integer itemId){
        if (itemId == null){
            return new HashMap<Object,Object>();
        }

        Items item = itemService.queryItemByItemId(itemId);
        List<ItemsImg> itemsImgs = itemService.queryItemImagByItemId(itemId);
        ItemsParam itemsParam = itemService.queryItemParamByItemId(itemId);
        List<ItemsSpec> itemsSpecs = itemService.queryItemSpecByItemId(itemId);
        Map<String,Object> result = new HashMap<>();
        result.put("item",item);
        result.put("itemsImgs",itemsImgs);
        result.put("itemsParam",itemsParam);
        result.put("itemsSpecs",itemsSpecs);
        return result;
    }

    @GetMapping("commentCounts/{itemId}")
    @ApiOperation(value = "根据商品id查询评价等级",httpMethod = "GET")
    public CommentLevelCountsVO commentCounts(@PathVariable Integer itemId){
        CommentLevelCountsVO commentLevelCountsVO = itemService.queryCommentCounts(itemId);
        return commentLevelCountsVO;
    }

    @GetMapping("comments/{itemId}/{level}")
    @ApiOperation(value = "根据商品id、评价等级查看评论",httpMethod = "GET")
    public PageInfo commentCounts(@PathVariable Integer itemId,@PathVariable Integer level,Integer pageNum,Integer pageSize){

        if (StringUtils.isBlank(itemId+"")){
            return new PageInfo();
        }
        if (StringUtils.isBlank(pageNum+"")){
            pageNum = 1;
        }
        if (StringUtils.isBlank(pageSize+"")){
            pageSize = 2;
        }

        PageInfo pageInfo = itemService.queryCommentsByItemIdAndLevel(itemId, level,pageNum,pageSize);
        return pageInfo;
    }

    @GetMapping("items")
    @ApiOperation(value = "根据商品名称、排序方式查找商品",httpMethod = "GET")
    public PageInfo items(String itemName, String sort, Integer pageNum, Integer pageSize){

//        if (StringUtils.isBlank(itemName)){
//            return new PageInfo();
//        }
        if (StringUtils.isBlank(sort)){
            sort = "k";
        }
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 2;
        }

        PageInfo pageInfo = itemService.queryItemByLikeItemName(pageNum,pageSize,itemName, sort);
        return pageInfo;
    }

    @GetMapping("items/{catId}")
    @ApiOperation(value = "根据商品id、排序方式查找商品",httpMethod = "GET")
    public PageInfo itemsByCatId(@PathVariable Integer catId, String sort, Integer pageNum, Integer pageSize){

//        if (StringUtils.isBlank(itemName)){
//            return new PageInfo();
//        }
        if (StringUtils.isBlank(sort)){
            sort = "k";
        }
        if (pageNum == null){
            pageNum = 1;
        }
        if (pageSize == null){
            pageSize = 2;
        }

        PageInfo pageInfo = itemService.queryItemByCatId(pageNum,pageSize,catId, sort);
        return pageInfo;
    }

    @GetMapping("shopCart")
    @ApiOperation(value = "根据商品规格ids 查找最新的商品规格信息")
    public List<ShopcartVO> refreshShopCart(@RequestParam @ApiParam(value = "用户id")String userId,@RequestParam @ApiParam(value = "商品的规格id",example = "1") String specId){
        if (StringUtils.isBlank(specId) || StringUtils.isBlank(userId)){
            return new ArrayList<>();
        }


        //todo:如果当前用户已经登录，则需要从redis中删除对应规格id的商品


        return new ArrayList<>();
    }


    @GetMapping("del")
    @ApiOperation(value = "根据商品规格id 删除购物车中的规格信息")
    public List<ShopcartVO> delShopCartBySpecId(@RequestParam @ApiParam(value = "商品的规格ids",example = "1,2,3") String specIds){
        if (StringUtils.isBlank(specIds)){
            return new ArrayList<>();
        }

        String[] split = specIds.split(",");
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,split);

        //todo:查询出来的购物车信息应该存放在redis中

        List<ShopcartVO> shopcartVOS = itemService.queryShopCartBySpecIds(list);
        return shopcartVOS;
    }


}
